﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Diagnostics;


namespace ToPinyin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        
        private void button1_Click(object sender, EventArgs e)
        {
            
            //获取文件列表
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                String path = folderBrowserDialog1.SelectedPath;
                DirectoryInfo sourcePath = new DirectoryInfo(path);
                FileInfo[] files = sourcePath.GetFiles("*.*");
                foreach (FileInfo i in files)
                {
                    //Console.WriteLine(i.FullName.ToString());
                    listBox1.Items.Add(i.FullName.ToString());
                }

            }

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {


            if (listBox1.Items.Count != 0 && File.Exists(listBox1.Items[0].ToString()))
            {

                string[] lb1 = new string[listBox1.Items.Count];
                listBox1.Items.CopyTo(lb1,0);

                for (int i=0;i < lb1.Length; i++)
                {

                    listBox1.SelectedIndex = i;
                    string sourc = lb1[i];
                    if (checkBox1.CheckState.ToString() == "Checked")
                    {
                        StreamReader sr = File.OpenText(sourc);
                        string line = "";
                        string result = "";
                        while ((line = sr.ReadLine()) !=null)
                        {
                            result = result +  data.GetPinYin(line,false) + Environment.NewLine;
                            Console.WriteLine(data.GetPinYin(line));
                               
                        }
                        sr.Close();
                        File.WriteAllText(sourc, result);
                        
                    }
                    
                    if (checkBox2.CheckState.ToString() == "Checked")
                    {
                        string name = data.GetPinYin(Path.GetFileName(sourc));
                        string dest = Path.GetDirectoryName(sourc) + "\\" + name;
                        File.Move(sourc, dest);
                        lb1[i] = dest;

                    }
                    

                }

                listBox1.Items.Clear();
                listBox1.Items.AddRange(lb1);
            } else
            {
                MessageBox.Show("没有要转换的文件！");
            }
            
        }
        

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Text = "加载字典中…………";
            data.GetPinYin("");
            this.Text = "字典加载完毕";
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            listBox1.Items.Clear();
            for (int i = 0; i < s.Length; i++)
            {
                listBox1.Items.Add(s[i]);
            }
        }


        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            form2 form2 = new form2();
            form2.Owner = this;
            form2.ShowDialog();
        }
    }
    
}
